Expand description
§TinyTemplate
TinyTemplate is a minimal templating library originally designed for use in Criterion.rs. It deliberately does not provide all of the features of a full-power template engine, but in return it provides a simple API, clear templating syntax, decent performance and very few dependencies.
§Features
The most important features are as follows (see the syntax module for full details on the template syntax):
- Rendering values -
{ myvalue }
- Conditionals -
{{ if foo }}Foo is true{{ else }}Foo is false{{ endif }}
- Loops -
{{ for value in row }}{value}{{ endfor }}
- Customizable value formatters
{ value | my_formatter }
- Macros
{{ call my_template with foo }}
§Restrictions
TinyTemplate was designed with the assumption that the templates are available as static strings,
either using string literals or the include_str!
macro. Thus, it borrows &str
slices from the
template text itself and uses them during the rendering process. Although it is possible to use
TinyTemplate with template strings loaded at runtime, this is not recommended.
Additionally, TinyTemplate can only render templates into Strings. If you need to render a template directly to a socket or file, TinyTemplate may not be right for you.
§Example
#[macro_use]
extern crate serde_derive;
extern crate tinytemplate;
use tinytemplate::TinyTemplate;
use std::error::Error;
#[derive(Serialize)]
struct Context {
name: String,
}
static TEMPLATE : &'static str = "Hello {name}!";
pub fn main() -> Result<(), Box<Error>> {
let mut tt = TinyTemplate::new();
tt.add_template("hello", TEMPLATE)?;
let context = Context {
name: "World".to_string(),
};
let rendered = tt.render("hello", &context)?;
println!("{}", rendered);
Ok(())
}
Modules§
- Module containing the error type returned by TinyTemplate if an error occurs.
- Documentation of TinyTemplate’s template syntax.
Structs§
- The TinyTemplate struct is the entry point for the TinyTemplate library. It contains the template and formatter registries and provides functions to render templates as well as to register templates and formatters.
Functions§
- Appends
value
tooutput
, performing HTML-escaping in the process. - The format function is used as the default value formatter for all values unless the user specifies another. It is provided publicly so that it can be called as part of custom formatters. Values are formatted as follows:
- Identical to
format
except that this does not perform HTML escaping.
Type Aliases§
- Type alias for closures which can be used as value formatters.